package vn.com.demand.widget;

import java.sql.SQLException;
import java.util.regex.Pattern;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import vn.com.demand.lux.model.CustomerBean;
import vn.com.demand.util.DpUtils;

public class SearcheableCustomerTable extends Composite {

	private TableColumn addressColumn;
	private Table table;
	private TableViewer tableViewer;
	private MyViewerFilter filter;
	private Text text;
	private ItemBrowserCommand command;
	private TableColumn customerCodeColumn;
	private TableColumn customerNameColumn;
	/**
	 * Create the composite
	 * 
	 * @param parent
	 * @param style
	 * @throws SQLException
	 */
	public SearcheableCustomerTable(Composite parent, int style, String sql,
			final Text text) throws SQLException {
		super(parent, style);
		setLayout(new FillLayout());

		command = new ItemBrowserCommand(sql, null, new CustomerBean());
		this.text = text;
		createCompositeContent();
		//
	}
	
	public SearcheableCustomerTable(Composite parent, int style, ItemBrowserCommand command,
			final Text text) throws SQLException {
		super(parent, style);
		setLayout(new FillLayout());
		this.command = command;
		this.text = text;
		createCompositeContent();
		//
	}

	private void createCompositeContent()
			throws SQLException {
		tableViewer = new TableViewer(this, getStyle());
		table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		
		customerCodeColumn = new TableColumn(table, SWT.NONE);
		customerCodeColumn.setWidth(121);
		customerCodeColumn.setText("Customer code");
		
		customerNameColumn = new TableColumn(table, SWT.NONE);
		customerNameColumn.setWidth(150);
		customerNameColumn.setText("Customer name");

		addressColumn = new TableColumn(table, SWT.NONE);
		addressColumn.setWidth(300);
		addressColumn.setText("Address");

		tableViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {

					public void selectionChanged(SelectionChangedEvent event) {
						TableItem[] selection = tableViewer.getTable()
								.getSelection();
						if (null != selection && selection.length > 0) {
							CustomerBean model = (CustomerBean) selection[0].getData();
							if (null != text) {
								text.setText(model.getCustomerCode());
							}
						}
					}

				});

		tableViewer.setContentProvider(new MyContentProvider());
		tableViewer.setLabelProvider(new MyLabelProvider());

		filter = new MyViewerFilter(null);
		tableViewer.setFilters(new ViewerFilter[] { filter });
		tableViewer.setInput(createModel());
	}

	private class MyViewerFilter extends ViewerFilter {

		private String filterValue;
		private Pattern customerCodePattern;
		private Pattern customerNamePattern;
		private Pattern addressPattern;

		public MyViewerFilter(String filterValue) {
			setFilterValue(filterValue);
		}

		@Override
		public boolean select(Viewer arg0, Object parentElement, Object element) {
			try {
				if (null == filterValue) {
					return true;
				}
				CustomerBean model = (CustomerBean) element;
				if ((customerCodePattern.matcher(model.getCustomerCode())
						.find())
						|| (customerNamePattern
								.matcher(model.getCustomerName()).find())
						|| (model.getAddress() != null && addressPattern.matcher(model.getAddress()).find())) {
					return true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}

		public void setFilterValue(String value) {
			filterValue = value;
			customerCodePattern = Pattern.compile(".*" + filterValue + ".*",
					Pattern.CASE_INSENSITIVE);
			customerNamePattern = Pattern.compile(".*" + filterValue + ".*",
					Pattern.CASE_INSENSITIVE);
			addressPattern = Pattern.compile(".*" + filterValue + ".*",
					Pattern.CASE_INSENSITIVE);
		}
	};

	private class MyContentProvider implements ITreeContentProvider {

		public Object[] getChildren(Object arg0) {
			return null;
		}

		public Object getParent(Object arg0) {
			return null;
		}

		public boolean hasChildren(Object arg0) {
			return false;
		}

		public Object[] getElements(Object myModelArr) {
			return (Object[]) myModelArr;
		}

		public void dispose() {

		}

		public void inputChanged(Viewer arg0, Object arg1, Object arg2) {

		}

	}

	private CustomerBean[] createModel() throws SQLException {
		return (CustomerBean[]) command.execute(new CustomerBean());
	}

	private class MyLabelProvider extends LabelProvider implements
			ITableLabelProvider {

		public Image getColumnImage(Object arg0, int arg1) {
			return null;
		}

		public String getColumnText(Object model, int column) {
			CustomerBean bean = (CustomerBean) model;
			switch (column) {
			case 0:
				return DpUtils.trim(bean.getCustomerCode());
			case 1:
				return DpUtils.trim(bean.getCustomerName());
			case 2:
				return DpUtils.trim(bean.getAddress());
			default:
				break;
			}
			return null;
		}

	}

	@Override
	public void dispose() {
		super.dispose();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public Object[] getChildren(Object arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object getParent(Object arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean hasChildren(Object arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public Object[] getElements(Object arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
		// TODO Auto-generated method stub

	}

	public String[] getSelectedItemsCode() {
		TableItem[] selection = tableViewer.getTable().getSelection();
		if (selection != null && selection.length > 0) {
			String[] results = new String[selection.length];
			for (int i = 0; i < selection.length; i++) {
				results[i] = ((CustomerBean) selection[i].getData()).getCustomerCode();
			}
			return results;
		}
		return null;
	}

	public CustomerBean getSelectedModel() {
		TableItem[] selection = tableViewer.getTable().getSelection();
		if (null != selection && selection.length > 0) {
			CustomerBean model = (CustomerBean) selection[0].getData();
			return model;
		}
		return null;
	}
	
	public void setSelectedItem(String value) {

	}

	public void setFilterValue(String value) {
		filter.setFilterValue(value);
		tableViewer.refresh();
	}
	
}
